<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="料号" prop="materialNo">
        <el-input
          v-model="queryParams.materialNo"
          placeholder="请输入料号"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['big/dcs/kitting:detail:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="downloadTemplate"
          >下载导入模板</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <MyUpload
          buttonName="导入模板"
          url="/"
          @success="handleUploadSuccess"
          @warning="handleUploadWarning"
        />
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="detailList"
      @selection-change="handleSelectionChange"
      show-overflow-tooltip
      :tooltip-formatter="tableRowFormatter"
      border
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column label="备料单号" align="center" prop="mergeNo" />
      <el-table-column label="料号" align="center" prop="materialNo" />
      <el-table-column
        label="备料单应发数量"
        align="center"
        prop="plannedQty"
      />
      <el-table-column label="单位" align="center" prop="unit" />
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['big/dcs/kitting:detail:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['big/dcs/kitting:detail:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改备料单单体（存储备料单的料号级明细信息）对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="detailRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="备料单号" prop="mergeNo">
          <el-input v-model="form.mergeNo" placeholder="请输入备料单号" />
        </el-form-item>
        <el-form-item label="料号" prop="materialNo">
          <el-input v-model="form.materialNo" placeholder="请输入料号" />
        </el-form-item>
        <el-form-item label="备料单应发数量" prop="plannedQty">
          <el-input
            v-model="form.plannedQty"
            placeholder="请输入备料单应发数量"
          />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="关联备料单单头表的主键id" prop="kittingOrderId">
          <el-input
            v-model="form.kittingOrderId"
            placeholder="请输入关联备料单单头表的主键id"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Detail">
import {
  listDetail,
  getDetail,
  delDetail,
  addDetail,
  updateDetail,
} from "@/api/work/prepareProgressDetail";

const { proxy } = getCurrentInstance();
const route = useRoute()
const detailList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    kittingOrderId: route.query.id || null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

const tableRowFormatter = (data) => {
  return `${data.cellValue}`
}

/** 查询备料单单体（存储备料单的料号级明细信息）列表 */
function getList() {
  loading.value = true;
  listDetail(queryParams.value).then((res) => {
    if (res.code === 200) {
      detailList.value = res.rows;
      total.value = res.total;
      loading.value = false;
    }
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    mergeNo: null,
    materialNo: null,
    plannedQty: null,
    unit: null,
    kittingOrderId: null,
    createBy: null,
    updateBy: null,
    createTime: null,
    updateTime: null,
  };
  proxy.resetForm("detailRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加备料单单体（存储备料单的料号级明细信息）";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getDetail(_id).then((res) => {
    form.value = res.data;
    open.value = true;
    title.value = "修改备料单单体（存储备料单的料号级明细信息）";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["detailRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateDetail(form.value).then((res) => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          } else {
            proxy.$modal.msgError(res.msg);
          }
        });
      } else {
        addDetail(form.value).then((res) => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          } else {
            proxy.$modal.msgError(res.msg);
          }
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm(
      '是否确认删除备料单单体（存储备料单的料号级明细信息）编号为"' +
        _ids +
        '"的数据项？'
    )
    .then(function () {
      return delDetail(_ids);
    })
    .then((res) => {
      if (res.code === 200) {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      } else {
        proxy.$modal.msgError(res.msg);
      }
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "big/dcs/kitting/detail/export",
    {
      ...queryParams.value,
    },
    `detail_${new Date().getTime()}.xlsx`
  );
}

getList();

// 下载模板
const downloadTemplate = async () => {
  proxy.download("/", {}, `detail模板_${new Date().getTime()}.xlsx`);
};
// 上传成功后的回调
const handleUploadSuccess = (res) => {
  getList();
};
// 部分成功回调
const handleUploadWarning = (res) => {
  getList();
};
</script>
